package com.heu.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.heu.reggie.common.BaseContext;
import com.heu.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: lidong
 * @description
 * @date: 2022/11/3 11:45
 */
@WebFilter(filterName = "loginCheckFilter" ,urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        log.info("拦截的请求:{}",request.getRequestURI());

        //获取请求url
        String requestURI = request.getRequestURI();

        //不需要请求的路径
        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"

        };
        //判断请求是否需要处理
        Boolean check = check(urls, requestURI);

        if (check){
            log.info("本次请求{}不需要处理",requestURI);
            chain.doFilter(request,response);
            return;
        }


        //后端登录
        if (request.getSession().getAttribute("employee")!=null){
            log.info("用户已登录");

            Long empId = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);

//            long id = Thread.currentThread().getId();
//            log.info("线程id为：{}",id);


            chain.doFilter(request,response);
            return;
        }

        //移动端判断是否登录
        if (request.getSession().getAttribute("user")!=null){
            log.info("用户已登录");

            Long userId = (Long) request.getSession().getAttribute("user");

            BaseContext.setCurrentId(userId);

//            long id = Thread.currentThread().getId();
//            log.info("线程id为：{}",id);


            chain.doFilter(request,response);
            return;
        }
        log.info("用户未登录");
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;

    }

    public Boolean check(String[] urls,String requestURL){
        for (String url:urls) {
            boolean match = PATH_MATCHER.match(url, requestURL);
            if (match){
                return true;
            }
        }
        return false;
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
